package com.cskaoyan.bean.wx_cart;

import lombok.Data;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartListAndPara {
    private List<Cart> cartList;
    private CartInfo cartTotal;

    public List<Cart> getCartList() {
        return cartList;
    }

    public void setCartList(List<Cart> cartList) {
        this.cartList = cartList;
        Map<String, Object> cartParam = getCartParam(cartList);
        CartInfo cartInfo = new CartInfo();
        cartInfo.setGoodsAmount((BigDecimal) cartParam.get("goodsAmount"));
        cartInfo.setCheckedGoodsAmount((BigDecimal) cartParam.get("checkedGoodsAmount"));
        cartInfo.setGoodsCount((int) cartParam.get("goodsCount"));
        cartInfo.setCheckedGoodsCount((int) cartParam.get("checkedGoodsCount"));
        this.cartTotal = cartInfo;
    }

    public CartInfo getCartTotal() {
        return this.cartTotal;
    }

    private Map<String, Object> getCartParam(List<Cart> carts) {
        int goodsCount = 0;
        int checkedGoodsCount = 0;
        BigDecimal goodsAmount = new BigDecimal(0);
        BigDecimal checkedGoodsAmount = new BigDecimal(0);
        for (Cart cart : carts) {
            goodsCount += cart.getNumber();
            goodsAmount=goodsAmount.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
            if (cart.getChecked()) {
                checkedGoodsCount += cart.getNumber();
                checkedGoodsAmount=checkedGoodsAmount.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsCount", goodsCount);
        map.put("checkedGoodsCount", checkedGoodsCount);
        map.put("goodsAmount", goodsAmount);
        map.put("checkedGoodsAmount", checkedGoodsAmount);
        return map;
    }
}
